package salary

// 薪酬分类
type PaymentClassification interface {
	CalculatePay(paycheck Paycheck) float64
	GetSalesReceipts() []SalesReceipt
	GetTimeCards() []TimeCard
}

type CommissionedClassification struct {
	PaymentClassification
	MonthlySalary  float64        //月收入
	CommissionRate float64        //佣金率
	SalesReceipts  []SalesReceipt //销售收据
}

func (c *CommissionedClassification) GetSalesReceipts() []SalesReceipt {
	return c.SalesReceipts
}

func (c *CommissionedClassification) CalculatePay(paycheck Paycheck) float64 {
	sum := 0.0
	receipts := c.SalesReceipts
	for _, receipt := range receipts {
		// 时间段内的销售数据, 使用佣金率*销售数量得出佣金, 再加上本有收入
		if IsBetween(receipt.Date, paycheck.PayStartTime, paycheck.PayDate) {
			sum += c.CommissionRate * receipt.Amount
		}
	}
	return c.MonthlySalary + sum
}

// 小时工
type HourlyClassification struct {
	PaymentClassification
	HourlyRate float64    // 时薪
	TimeCard   []TimeCard // 考勤
}

func (h *HourlyClassification) GetTimeCards() []TimeCard {
	return h.TimeCard
}

func (h *HourlyClassification) CalculatePay(paycheck Paycheck) float64 {
	sum := 0.0
	card := h.TimeCard
	for _, timeCard := range card {
		if IsBetween(timeCard.Date, paycheck.PayStartTime, paycheck.PayDate) {
			hours := timeCard.Hours
			if hours > 8 {
				// 八小时计算
				sum += h.HourlyRate * 8
				// 加班算一点五倍
				sum += h.HourlyRate * (hours - 8) * 1.5
				continue
			}
			// 把不足8的计算也加上
			sum += h.HourlyRate * hours
		}
	}
	return sum
}


// 月薪
type SalariedClassification struct {
	PaymentClassification
	MonthlySalary float64    // 时薪
}

func (h *SalariedClassification) CalculatePay(paycheck Paycheck) float64 {
	return h.MonthlySalary
}
